<?php

namespace common\models\datas;

use common\components\bases\BaseModel;
use common\components\bases\MsgtemplateValidator;
use common\components\extenders\MenuManage;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

class MsgTemplate extends BaseModel
{
    public $send_way;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%msg_template}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['msg_template_code', 'unique'],
            [['msg_template_name','msg_template_code','send_way','msg_template_event','receive_group'], 'required'],
            //[['sms_state', 'email_state', 'wechat_state', 'sys_state'], 'integer'],
            [['msg_template_name'], 'string', 'max' => 100],
            [['sms_text', 'email_text', 'wechat_text', 'sys_text'], 'string', 'max' => 500],
            [['sms_text','email_text', 'wechat_text', 'sys_text'],MsgtemplateValidator::className(),'whenClient' => "function (attribute, value) {
                return $('#msgtemplate-msg_template_event').val() != '';
            }"]
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };

        $fields['send_way'] = function () {
            return ['sms'];
//            $arr = [];
//            if($this->sms_text == 1){
//                $arr[] = 'sms';
//            }
//            if($this->email_state == 1){
//                $arr[] = 'email';
//            }
//            if($this->wechat_state == 1){
//                $arr[] = 'wechat';
//            }
//            if($this->sys_state == 1){
//                $arr[] = 'sys';
//            }
//            return $arr;

        };
        return $fields;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => \Yii::t('common', '自动编号'),
            'msg_template_name' => \Yii::t('common', '模板名称'),
            'msg_template_code' => \Yii::t('common', '模板码'),
            'sms_text' => \Yii::t('common', '短信模型内容'),
            'sms_state' => \Yii::t('common', '是否开启短信'),
            'email_text' => \Yii::t('common', '邮件内容'),
            'email_state' => \Yii::t('common', '是否发送邮件'),
            'wechat_text' => \Yii::t('common', '微信内容'),
            'wechat_state' => \Yii::t('common', '微信是否发送'),
            'sys_text' => \Yii::t('common', '系统消息'),
            'sys_state' => \Yii::t('common', '是否发送系统消息'),
            'admin_id' => \Yii::t('common', '管理员'),
            'created_at' => \Yii::t('common', '创建时间'),
            'send_way' => \Yii::t('common', '发送方式'),
            'msg_template_event' => \Yii::t('common', '消息类型'),
            'receive_group' => \Yii::t('common', '接收对象'),
        ];
    }

    public function extraFields()
    {
        return [
            'admin' => function () {
                return $this->admin->username;
            },
            'backend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();

            },

        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
//            [
//                'class' => UpdateCacheBehavior::className(),
//                'cacheName' => 'msgtemplate'
//            ]
        ]);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'admin_id']);
    }

//    public function getMessageConfig()
//    {
//        return $this->hasOne(MessageConfig::className(), ['id' => 'config_id']);
//    }
}